package org.springside.modules.security.springsecurity;

import org.springframework.security.Authentication;
import org.springframework.security.context.SecurityContextHolder;

import dapps.entity.user.User;

/**
 * Spring Security的工具类.

 */
public class SpringSecurityUtils {

	private SpringSecurityUtils() {
	}

	/**
	 * 取得当前用户的登录名,如果无已登录用户则返回null.
	 */
	public static String getCurrentUserName() {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		if (auth == null)
			return null;
		return auth.getName();
		

	}
	public static User getCurrentUser() {
		Object obj =SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		User currentuser;
		if (obj instanceof User) {
		    currentuser =((User)obj);
		} else {
			currentuser =null;
		}
		return currentuser;
		

	}
	
	
	
}
